<template>
  <div class="vue-city-backdrop" v-show="value">
    <div class="city-wrapper">
      <div class="city-content">
        <div class="city-box all-city-box">
          <div class="city-item">
            <span>选择地域</span>
          </div>
          <div class="city-item" :class="{active: province === item.code}" v-for="item in allCityList" :key="item.code" @click="onCity(item)">
            <span>{{item.name}}</span>
          </div>
        </div>
        <div class="city-box">
          <div class="city-item" :class="{active: city === item.code}" v-for="item in cityList" :key="item.code" @click="onArea(item)">
            <span>{{item.name}}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import cityData from '@/common/city.json'
import VueTab from '@/components/VueTab'
import scroll from '@/mixins/scroll'
export default {
  mixins: [scroll],
  components: {
    VueTab
  },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      allCityList: cityData,
      cityList: [],
      areaList: [],
      currentPage: 1,
      province: '',
      city: '',
      area: '',
      addressObj: {}
    }
  },
  watch: {
    value(newVal) {
      if (newVal) {
        this.scroll(true)
      } else {
        this.scroll(false)
      }
    },
  },
  mounted(){
    if(this.allCityList[0].name != '所有地域'){
      this.allCityList.unshift({
        "code": "",
        "name": "所有地域",
        "children": [
          {
            "code": "",
            "name": "",
          }
        ],
      })
    }
  },
  methods: {
    onCity(item) {
      if (this.province !== item.code) {
        this.cityList = []
      }
      this.province = item.code
      this.addressObj['city'] = this.province
      this.$nextTick(() => {
        if(item.children.length > 1 && item.children[0].code != item.code){
          item.children.unshift({
            "code": item.code,
            "name": "全" + item.name
          })
        }
        this.cityList = item.children
        this.city = item.children[0].code
        this.$emit('change', JSON.stringify(this.addressObj))
      })
    },
    onArea(item) {
      if (this.city !== item.code) {
        this.areaList = []
      }
      this.city = item.code
      this.addressObj['city'] = this.city
      this.$nextTick(() => {
        this.areaList = item.children
        this.$emit('change', JSON.stringify(this.addressObj))
      })
    },
  }
}
</script>
<style lang="scss">
.vue-city-backdrop {
  .tab-title-nav {
    background-color: #fff;
  }
  .tab-title-container {
    justify-content: flex-start;
  }
  .tab-title {
    flex: none;
    padding: 0 0.2rem;
    color: #000;
    &.active {
      color: #2460e5;
      span {
        width: 60%;
        height: 0.05rem;
        background-color: #2460e5;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
      }
    }
  }
}
</style>
<style lang="scss" scoped>
.vue-city-backdrop {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 99;
  .city-wrapper {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    background: transparent;
    transform: translate3d(0px, 0px, 0px);
    z-index: 99;
    .iconbtn-close {
      font-size: 0.32rem;
      position: absolute;
      right: 0.24rem;
      top: 0.2rem;
      z-index: 9999;
    }
    .city-content {
      position: relative;
      width: 100%;
      height: 100%;
      .city-box{
        width: 50%;
        float: left;
        height: 100%;
        overflow: hidden;
        overflow-y: auto;
      }
      .all-city-box{
        width: 45%;
        background: #f7f8fa;
          .city-item {
            &.active {
              &::before{
                content: '';
                display: inline-block;
                height: 0.42rem;
                border-left: 0.04rem solid #145afe;
                position: absolute;
                left: 0;
              }
            }
          }
      }
      .city-item {
        width: 100%;
        height: 0.8rem;
        line-height: 0.8rem;
        margin-top: 0.14rem;
        font-size: 0.26rem;
        display: inline-flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 0.24rem;
        position: relative;
        &.active {
          color: #2460e5;
          background: #ffffff;
        }
        .icongc-btn-skip {
          font-size: 0.24rem;
          color: #d9d9d9;
        }
      }
    }
  }
}
</style>